//
//  UITextField+Keyboard.m
//  KKCategoryKit
//
//  Created by liaozhenming on 16/6/7.
//  Copyright © 2016年 liaozhenming. All rights reserved.
//

#import "UITextField+Keyboard.h"
#import <objc/runtime.h>

@implementation UITextField (Keyboard)

+ (void)load {
    
    Method dealloc = class_getInstanceMethod([self class], NSSelectorFromString(@"dealloc"));
    Method keyboard_dealloc = class_getInstanceMethod([self class], @selector(keyboard_dealloc));
    method_exchangeImplementations(dealloc, keyboard_dealloc);
}

- (void)keyboard_dealloc {
    
    if ([self kk_keyboardEvent]) {
        [[NSNotificationCenter defaultCenter] removeObserver: self];
    }
    
    [self keyboard_dealloc];
}

#pragma mark - Setter methods
@dynamic kk_keyboardEvent;
static const void *UITextFieldKeyboardValue = &UITextFieldKeyboardValue;
- (void)setKk_keyboardEvent:(BOOL)kk_keyboardEvent {
    
    if (kk_keyboardEvent && self.kk_keyboardEvent == NO) {
        [self private_registerNotification];
    }
    objc_setAssociatedObject(self, UITextFieldKeyboardValue, @(kk_keyboardEvent), OBJC_ASSOCIATION_ASSIGN);
}

- (BOOL)kk_keyboardEvent {
    return [objc_getAssociatedObject(self, UITextFieldKeyboardValue) boolValue];
}

@dynamic kk_ownerView;
static const void *UITextfieldOwnerView = &UITextfieldOwnerView;
- (void)setKk_ownerView:(UIView *)kk_ownerView {
    objc_setAssociatedObject(self, UITextfieldOwnerView, kk_ownerView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (UIView*)kk_ownerView {
    return objc_getAssociatedObject(self, UITextfieldOwnerView);
}


#pragma mark - Notification methods

- (void)private_registerNotification {
    [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(private_keyboardDidChange:) name: UIKeyboardWillChangeFrameNotification object: nil];
    
    [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(private_keyboardWillHide:) name: UIKeyboardWillHideNotification object: nil];
}

- (void)private_keyboardWillHide:(NSNotification*)notification {
    if ([self isFirstResponder]) {
        [self.kk_ownerView setTransform: CGAffineTransformIdentity];
    }
}

- (void)private_keyboardDidChange:(NSNotification*)notification {
    
    if (!self.isFirstResponder) {
        return;
    }
    
    CGPoint keyboardCenter = [[notification.userInfo valueForKey: UIKeyboardCenterEndUserInfoKey] CGPointValue];
    if (keyboardCenter.y > CGRectGetHeight([[UIScreen mainScreen] bounds])) {
        return;
    }
    
    UIView *superView = self.superview;
    while (superView.superview) {
        superView = superView.superview;
    }
    [self setKk_ownerView: superView];
    CGRect convertFrame = [self.superview convertRect: self.frame toView: superView];
    
    CGRect keyboardBounds = [[notification.userInfo valueForKey: UIKeyboardBoundsUserInfoKey] CGRectValue];
    NSTimeInterval animationDuration = [[notification.userInfo valueForKey: UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    
    
    CGFloat offset = CGRectGetMaxY(convertFrame) + CGRectGetHeight(keyboardBounds) - (CGRectGetHeight(keyboardBounds)/2 + keyboardCenter.y);
    if (offset > 0) {
        CGAffineTransform transform = superView.transform;
        superView.transform = CGAffineTransformTranslate(transform, 0, -offset - transform.ty - 5);
    }
    else {
        superView.transform = CGAffineTransformIdentity;
    }
}

@end
